#region File Description
//-----------------------------------------------------------------------------
// MainForm.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System.Windows.Forms;
#endregion

namespace XGame.SpriteEditor
{
    // System.Drawing and the XNA Framework both define Color types.
    // To avoid conflicts, we define shortcut names for them both.
    using GdiColor = System.Drawing.Color;
    using XnaColor = Microsoft.Xna.Framework.Graphics.Color;
    using Microsoft.Xna.Framework.Graphics;
    using System;
    using Microsoft.Xna.Framework;

    
    /// <summary>
    /// Custom form provides the main user interface for the program.
    /// In this sample we used the designer to add a splitter pane to the form,
    /// which contains a SpriteFontControl and a SpinningTriangleControl.
    /// </summary>
    /// 

    public enum LoadFileType
    {
        NewSprite,
        SavedSprite
    }

   
    public partial class MainForm : Form
    {
        private LoadFileType xgFileType;


        public MainForm()
        {
            InitializeComponent();

            
        }

        private void staticSpriteToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            this.xgLoadFile.ShowDialog(this);
            this.xgFileType = LoadFileType.NewSprite;
        }

        private void xgLoadFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            OpenFileDialog dialog = (OpenFileDialog) sender;

            if (xgFileType == LoadFileType.NewSprite)
            {
                this.spritePreviewControl2.Sprite = new Sprite(Texture2D.FromFile(this.spritePreviewControl2.GraphicsDevice, dialog.FileName));
                this.xgSpriteCelHeight_tb.Text = this.spritePreviewControl2.Sprite.Height.ToString();
                this.xgSpriteCelWidth_tb.Text = this.spritePreviewControl2.Sprite.Width.ToString();
                this.xgSpriteCelXPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.X.ToString();
                this.xgSpriteCelYPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.Y.ToString();
                this.xgSpriteScaleX_tb.Text = this.spritePreviewControl2.Sprite.DestinationRect.Width.ToString();
                this.xgSpriteScaleY_tb.Text = this.spritePreviewControl2.Sprite.DestinationRect.Height.ToString();
                this.xgSpriteRotation_tb.Text = this.spritePreviewControl2.Sprite.Rotation.ToString();
                timer1.Start();
            }
            else if (xgFileType == LoadFileType.SavedSprite)
            {
                // seralize the sprite with the content loader here;
            }
            
            this.spritePreviewControl2.Sprite.setDesitinationRectPosition(new Vector2(spritePreviewControl2.Width / 2,spritePreviewControl2.Height / 2));
           

        }

        private void xgCelWidth_TextChanged(object sender, System.EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                int outInt;
                if (!int.TryParse(xgSpriteCelWidth_tb.Text, out outInt))
                    xgSpriteCelWidth_tb.Text = "";
                else
                {
                    spritePreviewControl2.Sprite.setSourceWidth(outInt, this.xgResizeOnChange.Checked);
                    spritePreviewControl2.Sprite.Origin = new Vector2((spritePreviewControl2.Sprite.SourceRect.X + spritePreviewControl2.Sprite.SourceRect.Width / 2), (spritePreviewControl2.Sprite.SourceRect.Y + spritePreviewControl2.Sprite.SourceRect.Height / 2));
                }
            }


            this.spritePreviewControl2.Refresh();
        }

        private void xgCelHeight_TextChanged(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {

                int outInt;
                if (!int.TryParse(xgSpriteCelHeight_tb.Text, out outInt))
                    xgSpriteCelHeight_tb.Text = "";
                else
                {
                    spritePreviewControl2.Sprite.setSourceHeight(outInt, this.xgResizeOnChange.Checked);
                    spritePreviewControl2.Sprite.Origin = new Vector2((spritePreviewControl2.Sprite.SourceRect.X + spritePreviewControl2.Sprite.SourceRect.Width / 2), (spritePreviewControl2.Sprite.SourceRect.Y + spritePreviewControl2.Sprite.SourceRect.Height / 2));

                }

                this.spritePreviewControl2.Refresh();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                float outFloatX;
                float outFloatY;

                if (float.TryParse(this.xgSpriteCelXPos_tb.Text, out outFloatX) && float.TryParse(this.xgSpriteCelYPos_tb.Text, out outFloatY))
                {

                    this.spritePreviewControl2.Sprite.setSourceRectPosition(new Vector2(outFloatX, outFloatY));

                }
                else
                {
                    xgSpriteCelXPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.X.ToString();
                    xgSpriteCelYPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.Y.ToString();
                }
            }


            this.spritePreviewControl2.Refresh();
        }

        private void xgCelY_TextChanged(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                float outFloatX;
                float outFloatY;

                if (float.TryParse(this.xgSpriteCelXPos_tb.Text, out outFloatX) && float.TryParse(this.xgSpriteCelYPos_tb.Text, out outFloatY))
                {

                    this.spritePreviewControl2.Sprite.setSourceRectPosition(new Vector2(outFloatX, outFloatY));

                }
                else
                {
                    xgSpriteCelXPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.X.ToString();
                    xgSpriteCelYPos_tb.Text = this.spritePreviewControl2.Sprite.SourceRect.Y.ToString();
                }
            }


            this.spritePreviewControl2.Refresh();

        }

        private void setDestinationHeight(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {

                int outInt;
                if (int.TryParse(xgSpriteScaleY_tb.Text, out outInt))
                    spritePreviewControl2.Sprite.setDestinationHeight(outInt, false);
                else
                    xgSpriteScaleX_tb.Text = spritePreviewControl2.Sprite.DestinationRect.Width.ToString();

            }
        }

        private void setDestinationWidth(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {

                int outInt;
                if (int.TryParse(xgSpriteScaleX_tb.Text, out outInt))
                {
                    spritePreviewControl2.Sprite.setDestinationWidth(outInt, false);
                }
                else
                    xgSpriteScaleX_tb.Text = spritePreviewControl2.Sprite.DestinationRect.Width.ToString();
            }

        }

        private void setDestinationScaleX(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                float outFloat;
                if (float.TryParse(xgSpriteScaleX_tb.Text, out outFloat))
                {

                }
                else
                {

                }
            }
        }

        private void setDestinationScaleY(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                float outFloat;
                if (float.TryParse(xgSpriteScaleY_tb.Text, out outFloat))
                {

                }
                else
                {

                }
            }
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            if (spritePreviewControl2.Sprite != null)
            {
                float outFloat;
                if (float.TryParse(xgSpriteRotation_tb.Text, out outFloat))
                {
                    spritePreviewControl2.Sprite.Rotation = MathHelper.ToRadians(outFloat);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        



        //private void xgCelYPos_Scroll(object sender, ScrollEventArgs e)
        //{
        //    float percentage = 0;

            

        //    if (xgCelYPos.Value > 0)
        //    {
        //        percentage = (float)xgCelYPos.Maximum / (float)xgCelYPos.Width;
        //    }

        //    // needs some error checking
        //    this.spritePreviewControl2.Sprite.setSourceRectPosition(new Vector2(spritePreviewControl2.Sprite.SourceRect.X, spritePreviewControl2.Sprite.Height * percentage));
        //    xgCelY.Text = (spritePreviewControl2.Sprite.Width * percentage).ToString();
        //    this.Refresh();

        //}

     

        


        

    }
}
